package com.leyuangame;

import com.leyuangame.core.BMDataContext;
import com.leyuangame.core.websocket.GameServer;
import com.leyuangame.core.websocket.handler.GameEventHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;

import java.io.IOException;


@EnableAutoConfiguration
@SpringBootApplication
@EnableAsync //开启异步任务
@EnableJpaRepositories("com.leyuangame.dao.repository.jpa")
//@EnableElasticsearchRepositories("com.leyuangame.dao.repository.es")
@Controller
public class Application extends SpringBootServletInitializer {


	public static void main(String[] args) {
		SpringApplication springApplication = new SpringApplication(Application.class) ;
		BMDataContext.setApplicationContext(springApplication.run(args));
	}
    @Bean(name="processExecutor")
    public TaskExecutor workExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setThreadNamePrefix("Async-");
        threadPoolTaskExecutor.setCorePoolSize(10);
        threadPoolTaskExecutor.setMaxPoolSize(20);
        threadPoolTaskExecutor.setQueueCapacity(600);
        threadPoolTaskExecutor.afterPropertiesSet();
        return threadPoolTaskExecutor;
    }


    @Autowired
    private GameEventHandler gameEventHandler;
    @Bean
    public GameServer gameServer(GameEventHandler gameEventHandler) throws IOException {
//        GameEventHandler gameEventHandler = new GameEventHandler();
        GameServer gameServer = new GameServer(9081,gameEventHandler);
        gameEventHandler.setServer(gameServer);
        return gameServer;
    }
}
